package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.utils.Constant;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hs on 2018/3/14.
 */

public class AllPageLeftAdapter extends RecyclerView.Adapter<AllPageLeftAdapter.ViewHolder> {
    private static String TAG = Constant.baddcoolio2tag?Constant.TAGPREFIXSTR + AllPageLeftAdapter.class.getSimpleName():AllPageLeftAdapter.class.getSimpleName();
    private List<String> strList=null;
    private Context mContext = null;
    private TextView selectTextView = null;
    private View.OnClickListener mOnClickListener = null;
    private View.OnKeyListener mOnKeyListener=null;
    private View.OnFocusChangeListener mOnFocusListener= null;

    public AllPageLeftAdapter(List<String> paramArrayList) {
        if (paramArrayList != null) {
            this.strList = paramArrayList;
        } else {
            this.strList = new ArrayList();
        }
    }

    @Override
    public AllPageLeftAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View convertView= LayoutInflater.from(mContext).inflate(R.layout.all_page_left_item, null);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(final AllPageLeftAdapter.ViewHolder holder, int position) {
        if(position<strList.size()) {
            holder.itemView.setTag(position);
            String name = strList.get(position);
            holder.nameText.setText(name);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //LogUtil.i(TAG,"view="+view);
                    if(mOnClickListener!=null) {
                        mOnClickListener.onClick(view);
                    }
                }
            });


            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(b) {
                        holder.itemView.setBackgroundResource(R.drawable.new_movelist_left_selector);
                    } else {
                        holder.itemView.setBackgroundResource(R.color.transparent);
                    }
                    if(mOnFocusListener!=null) {
                        mOnFocusListener.onFocusChange(view,b);
                    }
                }
            });

            if(mOnKeyListener!=null) {
                holder.itemView.setOnKeyListener(mOnKeyListener);
            }
        }
    }

    @Override
    public int getItemCount() {
        if(strList!=null) {
            return strList.size();
        }
        return 0;
    }

    public void setOnRightKeyListener(View.OnKeyListener listener) {
        mOnKeyListener = listener;
    }

    public void setOnClickListenter( View.OnClickListener listenter) {
        mOnClickListener = listenter;
    }

    public void setOnFocusListener(View.OnFocusChangeListener listener) {
        mOnFocusListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        public ViewHolder(View itemView) {
            super(itemView);
            nameText=(TextView) itemView.findViewById(R.id.all_page_item_name);
        }
    }
}
